/**
 * Created by FDG296 on 2017/8/29.
 */
$(document).ready(function(){
    $.fn.dataTable.ext.errMode = 'none';
    $('#table_list').on( 'error.dt', function ( e, settings, techNote, message ) {
        $.jBox.error('系统异常，请联系管理员！', '错误');
        return false;
    } ).DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'/static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/goods-match/get-goods-list',
            type:'post',
            data:function(d){
                d.search_set_id = $('#search_set_id').val();
                d.type = $('#type').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "main_goods_id", "bSortable":false },
            { "data": "goods_id", "bSortable":false },
            { "data": "sku", "bSortable":false },
            { "data": "goods_name_cn", "bSortable":false },
            { "data": "sort", "bSortable":true },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="delGoods('+ full.id +')">删除</a>';
                }
            },
            {
                "targets": [6],
                "data": "sort",
                "render": function(data, type, full) {
                    return '<input name="update_sort" data-id="'+full.id+'" style="width:50px;" id="update_sort" value="'+data+'">&nbsp;&nbsp;&nbsp;' +
                        '<a href="javascript:;" class="sort_change">修改</a>';
                }
            },
            {
                "targets": [7],
                "data": "use_flg",
                "render": function(data, type, full) {
//                    var  title = '';
//                    if(data == 1){
//                        title = '禁用';
//                    }else{
//                        title = '启用';
//                    }
//                    return '<a href="javascript:" onclick="statusClick('+ full.id +','+ data + ')">' + title + '</a>';
                	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+data+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>'
                }
            }
        ],
        order: [[ 1, "desc" ]]                      //初始指定排序字段
    });
});

$('table').delegate('.sort_change','click',function(){
    var update_id = $(this).prev().attr("data-id");
    var sort = $(this).prev().val();
    $.post('/goods-match/mod-goods-sort',{update_id:update_id,sort:sort},success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function(){
                reload();
            },1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

//查询
$('#select_button').click(function(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})

$('#reset_button').click(function(){
    //$('#search_set_name').val('');
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})

//添加
$('#add_button').click(function(){
    $('#input_table').empty();
    $('#input_table').append('<tr> <th>物料号</th> <th>操作</th> </tr>');
    $('#myModal').modal('show');
});

//返回
$('#back_button').click(function(){
    history.back();
});

$('#add_input').click(function(){
    var tr_length = $('#input_table').find('tr').length;

    var tr_length_ot = 0;

    $('#input_table').find('tr').each(function(){
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length != undefined){
            tr_length_ot = eval(data_length);
        }
    })

    if(eval(tr_length_ot) > 0){
        tr_length = tr_length_ot + 1;
    }

    var html = '<tr data-length="'+tr_length+'"><td>'+
        '<input name="sku[]" class="form-control">'+
        '<input class="input_table_sort" value="'+tr_length+'" name="add_sort[]" type="hidden"></td>'+
        '<td><a href="javascript:;" class="up_input_table_tr">上移</a>'+
        ' <a href="javascript:;" class="down_input_table_tr">下移</a>'+
        ' <a class="del_input_table_tr" href="javascript:;">删除</a></td> </tr>';

    $('#input_table').append(html);
})

$('table').delegate('.del_input_table_tr','click',function(){
    $(this).parent().parent().remove();
})

$('table').delegate('.up_input_table_tr','click',function(){
    var tr_index = $(this).parent().parent().prev().index();
    if(eval(tr_index) == 0){
        return true;
    }else{
        var obj = $(this).parent().parent().clone();
        $(this).parent().parent().prev().before(obj);
        $(this).parent().parent().remove();
    }

    $('#input_table').find('tr').each(function(){
        var tr_index = $(this).index();
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length!= undefined && data_length != 0){
            $(this).attr('data-length',tr_index);
            $(this).find('.input_table_sort').val(tr_index);
        }
    })
})

$('table').delegate('.down_input_table_tr','click',function(){
    var tr_index = $(this).parent().parent().next().index();
    if(eval(tr_index) > eval(-1)){
        var obj = $(this).parent().parent().clone();
        $(this).parent().parent().next().after(obj);
        $(this).parent().parent().remove();
    }

    $('#input_table').find('tr').each(function(){
        var tr_index = $(this).index();
        var data_length = $(this).attr('data-length');
        if(data_length != '' && data_length!= undefined && data_length != 0){
            $(this).attr('data-length',tr_index);
            $(this).find('.input_table_sort').val(tr_index);
        }
    })
})

//重新加载数据
function reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}

$('.ajax_submit').click(function(){
    $.post($('#goods_form').attr('action'),$('#goods_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function(){
                $('#myModal').modal('hide');
                reload();
            },1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

//启用禁用
function statusClick(id,use_flg){
    var title = '';
    var postUrl = '/goods-match/change-relation-useflg';

    if(use_flg == 1){
        title = '确定禁用吗？';
        use_flg = 0;
    }else{
        title = '确定启用吗？';
        use_flg = 1;
    }

    $.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post(postUrl,{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();                             //关闭对话框
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () {
                        reload();
                    }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

//删除
function delGoods(id){
    $.jBox.confirm('确定删除吗？', "提示", function(v){
        if (v == 'ok'){
            $.post('/goods-match/del-goods-relation',{id:id},success,'json');
            return false;
            function success(data){
                $.jBox.close();                             //关闭对话框
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () {
                        reload();
                    }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
        }
    });
}

$('#table_list').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	statusClick(id,use_flg);
});